<?php

namespace App\Repository;

use App\Services\Resizer;

class UploadRepository extends BaseRepository
{
    public $upload = './attach';

    public function __construct()
    {
        parent::__construct();
    }

    public function upload($request, $key = 'photo', $isResizer = false, $shop_id = '1', $prx = 'image')
    {
        $list = array();
        if ($request->hasFile($key)) {
            $file = $request->file($key);
            // var_dump($file, 444444444);
            $dir = '/' . $shop_id . '/' . $prx . '/' . $key . '/' . date('Ym'); //Log::info('文件='.json_encode($file));

            if ($file->getClientOriginalExtension() == "php") return $list;
            $fileSize = getimagesize($file);
            // var_dump($fileSize);
            $fileS = $file->getSize();
            if ($fileS > 100000000) {
                return [$key . '_src' => '', 'msg' => '文件太大,文件最大在100M'];
            }
            // var_dump($fileS, 1111111111);
            // var_dump($file->getClientOriginalExtension(), 555555555);
            $houzui = $file->getClientOriginalExtension() ? $file->getClientOriginalExtension() : 'jpeg';
            // $houzui =  'png';
            if ($isResizer) {
                $houzui = 'png';
            }
            confirmDir($this->upload . $dir);
            $originalName = $file->getClientOriginalName();
            if ($key == 'recordFile' && !empty($request['callId'])) {
                $fileName = $request['callId'] . '.' . $houzui;
            } else {
                $fileName = date("YmdHmis") . str_pad(mt_rand(1, 999999), 6, 0, STR_PAD_LEFT) . '.' . $houzui;
            }
            $nowFile = substr($this->upload . $dir . '/' . $fileName, 1);
            if (!file_exists($nowFile)) {
                $file->move($this->upload . $dir, $fileName);
            }
            $list[$key . '_src'] = $nowFile;
            $list[$key . '_name'] = $originalName;
            if ($isResizer) {
                //缩小文件
                $newWidth = $fileSize[0] > 80 ? 80 : $fileSize[0];
                $newHeight = $fileSize[1] > 100 ? 100 : $fileSize[1];
                $resizer = new Resizer($this->upload . $dir . '/' . $fileName);
                $resizer->resize($newHeight, $newWidth);
                $resizer->save($this->upload . $dir . '/re_' . $fileName);
                $list[$key] = substr($this->upload . $dir . '/re_' . $fileName, 1); //Log::info('图2'.$list[$key]);
            }
        }
        return $list;
    }

    // 保存图片
    public function uploadBase64_back($data)
    {
        if (empty($data)) {
            return '';
        }

        //将图片流写入图片文件
        $dir = '/files' . '/' . date('Ymd');
        $fileName = date("YmdHmis") . str_pad(mt_rand(1, 999999), 6, 0, STR_PAD_LEFT) . '.png';
        $file = $this->upload . $dir . '/' . $fileName;
        // $file = $file_path . '/' . $file_name;
        if (file_exists($file)) {
            unlink($file);
        }
        confirmDir($this->upload . $dir);
        $newWidth = 80;
        $newHeight = 100;
        $resizer = new Resizer($data);
        $resizer->resize($newHeight, $newWidth);
        $resizer->save($file);
        // $image = file_get_contents($data);
        // if (@$fp = fopen($file, 'w+')) {
        //   fwrite($fp, $image);
        //   fclose($fp);
        // }
        return trim($file, '.');
    }

    public function uploadBase64($data, $fileName = '')
    {
        if (empty($data)) {
            return '';
        }
        // var_dump($data);
        if (is_string($data) && file_exists('.' . $data)) {
            return '';
        }
        //将图片流写入图片文件
        if (!empty($fileName)) {
            $dir = '/' . $fileName . '/' . date('Ymd');
        } else {
            $dir = '/files' . '/' . date('Ymd');
        }

        $fileName = date("YmdHmis") . str_pad(mt_rand(1, 999999), 6, 0, STR_PAD_LEFT) . '.png';
        $file = $this->upload . $dir . '/' . $fileName;
        confirmDir($this->upload . $dir);
        // $file = $file_path . '/' . $file_name;
        // var_dump($file, 5555555);
        if (file_exists($file)) {
            unlink($file);
        }

        // 将字节流处理
        if (strstr($data, ',')) {
            $dataArr = explode(',', $data);
            $data = $dataArr[1];
        }
        $res = file_put_contents($file, base64_decode($data));

        return trim($file, '.');
    }
}
